package springboot_demo.itheima.controller;

import com.sun.net.httpserver.HttpsServer;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springboot_demo.itheima.pojo.Article;
import springboot_demo.itheima.pojo.PageBean;
import springboot_demo.itheima.pojo.Result;
import springboot_demo.itheima.service.ArticleService;
import springboot_demo.itheima.utils.JwtUtil;

import java.util.Map;

/**
 * 功能：控制器层（处理HTTP请求）
 * 作者：Java课程项目组
 * 日期：2025年01月07日
 */
@RestController
@CrossOrigin//解决跨域问题
@RequestMapping("/article")
//访问文章内容接口
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping
    //新增文章接口
    public Result add(@RequestBody @Validated Article article) {
        articleService.add(article);
        return Result.success();
    }

    //文章列表(条件分页)接口
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ) {
        PageBean<Article> pb = articleService.list(pageNum, pageSize, categoryId, state);
        return Result.success(pb);
    }

    //获取文章详情接口
    @GetMapping("/detail")
    public Result<Article> detail(Integer id){
        Article a=articleService.findById(id);
        return Result.success(a);
    }

    //更新文章接口
    @PutMapping
    public Result update(@RequestBody Article article){
        articleService.update(article);
        return Result.success();
    }

    //删除文章接口
    @DeleteMapping
    public Result delete(Integer id){
        articleService.delete(id);
        return Result.success();
    }
}
